package com.mijie.core;

import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

public class CoreContextLoaderListener implements ServletContextListener{

	private static Logger logger = Logger.getLogger(CoreContextLoaderListener.class);
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		logger.info("=======================contextDestroyed===========================");
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		loadApplicationProperties(sce);
		loadXmlProperties(sce);
		initProxool(sce);
		initStringFilter(sce);
		loadRegionInfo(sce);
		logger.info("=======================contextInitialized===========================");
	}
	
	private void loadRegionInfo(ServletContextEvent sce){
		String flag = sce.getServletContext().getInitParameter("initRegionInfo");
		if(flag!=null && !flag.trim().isEmpty()){
			ApplicationProperties.INSTANCE.loadRegionInfo();
		}else{
			logger.info("####no need to loadRegionInfo");
		}
	}
	
	private void initStringFilter(ServletContextEvent sce){
		String wordsFile = getConfFilePath(sce,"disabledWords","/WEB-INF/classes/disabled_words.txt");
		File file = new File(wordsFile);
		if(file.exists()){
			StringFilter.init(file);
		}
	}
	
	private void loadApplicationProperties(ServletContextEvent sce){
		String appFile = getConfFilePath(sce,"applicationProperties","/WEB-INF/classes/application.properties");
		File file = new File(appFile);
		if(file.exists()){
			logger.info("####application.roperties location -> "+appFile);
			ApplicationProperties.INSTANCE.load(file);
		}else{
			logger.info("####can't found the application.properties");
		}
		
	}
	
	private void loadXmlProperties(ServletContextEvent sce){
		String xmlFile = getConfFilePath(sce,"xmlProperties","/WEB-INF/classes/properties.xml");
		File file = new File(xmlFile);
		if(file.exists()){
			logger.info("####properties.xml location -> "+xmlFile);
			ApplicationProperties.INSTANCE.loadXML(file);
		}else{
			logger.info("####can't found the properties.xml");
		}
		
	}
	
	private void initProxool(ServletContextEvent sce){
		String xmlFile = getConfFilePath(sce,"proxoolConfigLocation","/WEB-INF/classes/proxool.xml");
		File file = new File(xmlFile);
		if(!file.exists()){
			logger.info("####can't found the proxool config file");
			return;
		}
		logger.info("####proxool config location -> "+xmlFile);
		try {
			JAXPConfigurator.configure(xmlFile, false);
		} catch (ProxoolException e) {
			logger.error(e.getMessage(), e);
		}
	}

	private String getConfFilePath(ServletContextEvent sce,String servletContextInitParameterName,String defaultValue){
		String file = null;
		ServletContext sc = sce.getServletContext();
		String configLocation = sc.getInitParameter(servletContextInitParameterName);
		if(configLocation!=null){
			if(configLocation.startsWith("classpath:")){
				file = configLocation.substring(configLocation.indexOf("classpath:")+10);
				file = "WEB-INF/classes/"+file;
			}
		}
		if(file==null) file = defaultValue;
		file = sc.getRealPath(file);
		return file;
	}
}
